import { onUnmounted } from "vue"

export const useTimeout = () => {
  let timer: number

  function start(fn: any, wait: any) {
    if(timer) clearTimeout
    console.log(new Date().getTime())
    const startTime = new Date().getTime();
    fn()
    // 获取函数执行后的时间戳
    const endTime = new Date().getTime();
    // 计算函数执行时间
    const duration = endTime - startTime;

    console.log(`函数执行时间为 ${duration} ms`);
    timer = setTimeout(() => {
      start(fn, 98-duration)
    }, wait)
  }
  onUnmounted(() => {
    clearTimeout(timer)
  })
  return {
    start
  }
}
